package linkwechat.serviceimp;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import linkwechat.mapper.system.SysUserMapper;
import linkwechat.mapper.system.SysUserPostMapper;
import linkwechat.mapper.system.SysUserRoleMapper;
import linkwechat.pojo.LinkRole;
import linkwechat.pojo.SysUser;
import linkwechat.pojo.SysUserPost;
import linkwechat.pojo.SysUserRole;
import linkwechat.sysservice.SysUserService;
import linkwechat.util.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class SysUserServiceImp extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {


    @Resource
    SysUserMapper sysUserMapper;


    @Resource
    SysUserPostMapper sysUserPostMapper;

    @Resource
    SysUserRoleMapper sysUserRoleMapper;




    //  查询所有用户
    @Override
    public PageInfo<SysUser> selectUserList(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<SysUser> sysUsers = sysUserMapper.selectUserList();
        PageInfo<SysUser> pageInfo = new PageInfo<>(sysUsers);
        return pageInfo;
    }

    //    根据部门查询用户
    @Override
    public PageInfo<SysUser> selectDeptLike(Integer deptId, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<SysUser> sysUsers = sysUserMapper.selectDeptLike(deptId);
        PageInfo<SysUser> pageInfo = new PageInfo<>(sysUsers);
        return pageInfo;
    }

    //    用户条件查询
    @Override
    public List<SysUser> selectUserLike(SysUser sysUser, String startTime, String endTime) {
        return sysUserMapper.selectUserLike(sysUser, startTime, endTime);
    }

    //  查询所有角色
    @Override
    public List<LinkRole> selectRoleList() {
        return sysUserMapper.selectRoleList();
    }


    //   查询用户名 手机号 邮箱 是否唯一
    @Override
    public List<SysUser> selectUserUnique(SysUser sysUser) {
        return sysUserMapper.selectUserUnique(sysUser);
    }


    //   新增用户
    @Override
    public int insertUser(SysUser sysUser) {
        int i = sysUserMapper.insertUser(sysUser);
        // 新增用户岗位关联
        insertUserPost(sysUser);
        insertUserRole(sysUser);
        return i;
    }

    //     修改用户信息
    @Override
    public int updateUser(SysUser user) {
        Long userId = user.getUserId();
        // 删除用户与角色关联
        sysUserRoleMapper.deleteUserRoleByUserId(userId);
        // 新增用户与角色管理
        insertUserRole(user);
        // 删除用户与岗位关联
        sysUserPostMapper.deleteUserPostByUserId(userId);
        // 新增用户与岗位管理
        insertUserPost(user);
        return sysUserMapper.updateUser(user);
    }


    @Override
    public int updateUserStatus(SysUser sysUser) {
        return sysUserMapper.updateUserStatus(sysUser);
    }


    @Override
    public int deleteUserById(Long userId) {
        return sysUserMapper.deleteUserById(userId);
    }


    @Override
    public int deleteUserByIds(Long[] userIds) {
        return sysUserMapper.deleteUserByIds(userIds);
    }


    @Override
    public int registerUser(SysUser sysUser) {
        return sysUserMapper.registerUser(sysUser);
    }


    //   新增用户岗位信息
    public void insertUserPost(SysUser user) {
        Long[] posts = user.getPostIds();
        if (StringUtils.isNotNull(posts)) {
            // 新增用户与岗位管理
            List<SysUserPost> list = new ArrayList<>();
            for (Long postId : posts) {
                SysUserPost up = new SysUserPost();
                up.setUserId(user.getUserId());
                up.setPostId(postId);
                list.add(up);
            }
            if (list.size() > 0) {
                sysUserPostMapper.batchUserPost(list);
            }
        }
    }


    //  新增用户角色信息
    public void insertUserRole(SysUser user) {
        Long[] roles = user.getRoleIds();
        if (StringUtils.isNotNull(roles)) {
            // 新增用户与角色管理
            List<SysUserRole> list = new ArrayList<>();
            for (Long roleId : roles) {
                SysUserRole ur = new SysUserRole();
                ur.setUserId(user.getUserId());
                ur.setRoleId(roleId);
                list.add(ur);
            }
            if (list.size() > 0) {
                sysUserRoleMapper.batchUserRole(list);
            }
        }
    }


}
